package xyz.codingli.filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-16 15:02
 */
public class PfmFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("PfmFilter.init");
        Enumeration<String> names = filterConfig.getInitParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = filterConfig.getInitParameter(name);
            System.out.print("name:" + name + " value:" + value);
        }
        System.out.println();
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String uri = req.getRequestURI();
        long start = System.currentTimeMillis();
        chain.doFilter(servletRequest, servletResponse);
        long end = System.currentTimeMillis();
        System.out.println("PfmFilter.doFilter, uri:" + uri + " 耗时：" + (end - start) + " ms");

    }

    @Override
    public void destroy() {
        System.out.println("PfmFilter.destroy");
    }
}
